//
//  CFSheetView.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/5/2.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFSheetView.h"
#import "CFSheetCell.h"

@interface CFSheetView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) NSArray * titleArray;
@property (nonatomic ,strong) UITableView * tableView;

@end
@implementation CFSheetView

+ (CFSheetView *)sharedInstance{
    static  CFSheetView * sheetView;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sheetView = [[CFSheetView alloc] init];
    });
    return sheetView;
}

- (instancetype)init{
    if (self = [super init]) {
        [self initView];
    }
    return self;
}

+ (void)sheetViewWithTitleArray:(NSArray *)titleArray index:(selectedBlock)block{
    [[CFSheetView sharedInstance] sheetViewWithArrat:titleArray index:block];
}

- (void)sheetViewWithArrat:(NSArray *)titleArray index:(selectedBlock)block{
    _block = block;
    self.titleArray = titleArray;
    [self.tableView reloadData];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];

    CGFloat tableHeight = 50*(titleArray.count+1)+5;
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, DEF_SCREEN_HEIGHT()-tableHeight, DEF_SCREEN_WIDTH(), tableHeight);
    }];
     [DEF_WINDOW addSubview:self];
}

- (void)hidden{
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
        self.tableView.frame = CGRectMake(0, DEF_SCREEN_HEIGHT(), DEF_SCREEN_WIDTH(), 0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)initView{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
    self.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH(), DEF_SCREEN_HEIGHT());
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT(), DEF_SCREEN_WIDTH(), 0) style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.scrollEnabled = NO;
    [self addSubview:tableView];
    self.tableView = tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cellId = @"CFSheetCell";
    CFSheetCell * cell =  [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[CFSheetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    if (indexPath.section == 0) {
        cell.titleLabel.text = _titleArray[indexPath.row];
    }else{
        cell.titleLabel.text = @"取消";
    }

    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithHexString:@"000407"];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (_block) {
            _block(indexPath.row);
        }
        [self hidden];
    }else{
        [self hidden];
    }
}

@end
